home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-02
/
prodpack.zip
/
DB4PPSRC.EXE
/
_STRTRAN.PRG
< prev
next >
Wrap
Text File
|
1993-05-04
|
3KB
|
106 lines
FUNCTION _StrTran && Performs string search and replace operations
PARAMETERS pc_target, pc_locate, pc_replace, pn_first, pn_times
*--------------------------------------------------------------------
* NAME
* _STRTRAN - performs a search and replace on a
* character string.
*
* SYNOPSIS
* _STRTRAN( pc_target, pc_locate, pc_replace
* [, pn_first [, pn_times ] ] )
*
* DESCRIPTION
* _STRTRAN() allows a variety of search and replace
* operations to be performed on a string. Unlike
* STUFF(), it can perform repeated replacements
* within the target string.
*
* _STRTRAN() is similar to STUFF(), except that it
* also allows you to specify when to begin replacing
* the string, and how many times to replace it.
*
* If no occurrences of the pc_locate string are
* found, the pc_target string will be returned
* unchanged.
*
* Only the first three parameters are required.
* If pn_first and pn_times are not passed, _StrTran
* will start at the first occurrence and replace all.
* If pn_times is not passed, _StrTran will replace
* all by default.
*
* PARAMETERS
* pc_target = character string to search in
* pc_locate = character string to look for
* pc_replace = replacement string
* pn_first = number of first occurrence to replace
* pn_times = number of occurrences to replace
*
* EXAMPLE
*
* STORE "This_is_a_test" TO mvar
*
* * Replace every occurrence of "_":
* ? _STRTRAN( mvar, "_", " ", 1, 3 )
* * Returns "This is a test"
*
* * Replace every occurrence of "_":
* ? _STRTRAN( mvar, "_", " ", 1, 500 )
* * Returns "This is a test"
*
* * Replace second occurrence of "_":
* ? _STRTRAN( mvar, "_", "X", 2, 1 )
* * Returns "This_isXa_test"
*
* SEE ALSO:
* AT(), STUFF()
*
*--------------------------------------------------------------------
PRIVATE lc_target, ln_first, ln_foundct, ln_incr, ln_index, ln_loclen, ;
ln_patch, ln_replct, ln_replen, ln_times, nLenTarget
ln_foundct = 0
ln_replct = 0
ln_patch = 0
ln_index = 1
lc_target = pc_target
nLenTarget = LEN( pc_target )
ln_first = IIF( PCOUNT() = 3, 1, m->pn_first )
ln_times = IIF( PCOUNT() < 5, 255, m->pn_times )
ln_loclen = LEN(m->pc_locate)
ln_replen = LEN(m->pc_replace)
DO WHILE ln_index <= nLenTarget
ln_patch = AT(m->pc_locate, SUBSTR(m->lc_target, m->ln_index) )
IF m->ln_patch = 0
EXIT
ELSE
ln_index = ( m->ln_index - 1 ) + m->ln_patch
ln_foundct = m->ln_foundct + 1
IF m->ln_foundct >= m->ln_first
IF m->ln_replct < m->ln_times
lc_target = STUFF(m->lc_target, m->ln_index, ;
m->ln_loclen, m->pc_replace)
ln_replct = m->ln_replct + 1
ln_incr = m->ln_replen
ELSE
EXIT
ENDIF
ELSE
ln_incr = m->ln_loclen
ENDIF
ln_index = ( m->ln_index + m->ln_incr )
ENDIF
ENDDO
RETURN( m->lc_target )
*-- EOF: _StrTran( pc_target, pc_locate, pc_replace, pn_first, pn_times )